package com.example.teaculture.DB;

import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;

public class AdviceSQL {

    static DBOpenHelper helper;


    public static void initTable(Context context){
        helper = new DBOpenHelper(context);
        helper.getReadableDatabase();
    }

    /**【插入数据】**/
    public static void insert(Context context , Object [] data){

        Log.i("TAG:","插入数据到数据库表Advice中:"+data.toString());

        AdviceDao sqlManager = new AdviceDao(context);
        helper = new DBOpenHelper(context);
        helper.getWritableDatabase();
        String sql = "insert into Advice ( advice ) values ( ? )";
        Object [] bindArgs = data;
        sqlManager.updateSQLite( sql , bindArgs );
    }


    /**【模糊查询】**/
    public static ArrayList<HashMap<String ,String>>query(Context context){
        AdviceDao sqlManager = new AdviceDao(context);
        ArrayList<HashMap<String ,String>> list = new ArrayList<>();
        String sql = "select * from Advice";
        list = sqlManager.querySQLite(sql,null);
        Log.i("TAG:","查询完毕，返回数据：" + list.size());

        return list;
    }

    /**【删除数据】**/
    public static void delete(Context context , Object[] data){
        AdviceDao sqlmanager = new AdviceDao(context);
        String sql = "delete from Advice where advice =  ? ";
        sqlmanager.updateSQLite(sql , data);
    }

    /**【更新数据】**/
    public static void update(Context context , Object[]data){
        helper = new DBOpenHelper(context);
        helper.getReadableDatabase();
        AdviceDao sqlManager = new AdviceDao(context);
        String sql = "update Advice set flag=? where advice=?";
        sqlManager.updateSQLite(sql,data);
    }

}
